package com.bishe.app.modules.user.service.impl;

import com.bishe.app.core.enums.IsDel;
import com.bishe.app.modules.group.entity.Group;
import com.bishe.app.modules.group.service.GroupService;
import com.bishe.app.modules.groupuser.entity.GroupUser;
import com.bishe.app.modules.groupuser.service.GroupUserService;
import com.bishe.app.modules.menu.entity.Menu;
import com.bishe.app.modules.menu.entity.MenuTree;
import com.bishe.app.modules.menu.service.MenuService;
import com.bishe.app.modules.role.entity.Role;
import com.bishe.app.modules.role.service.RoleService;
import com.bishe.app.modules.rolemenu.entity.RoleMenu;
import com.bishe.app.modules.rolemenu.service.RoleMenuService;
import com.bishe.app.modules.user.entity.User;
import com.bishe.app.modules.user.mapper.UserMapper;
import com.bishe.app.modules.user.model.UserDTO;
import com.bishe.app.modules.user.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bishe.app.modules.userrole.entity.UserRole;
import com.bishe.app.modules.userrole.service.UserRoleService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author
 * @since 2024-03-13
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Resource
    private UserRoleService userRoleService;
    @Resource
    private RoleService roleService;
    @Resource
    private GroupService groupService;
    @Resource
    private MenuService menuService;
    @Resource
    private RoleMenuService roleMenuService;
    @Resource
    private GroupUserService groupUserService;

    @Value("${super.usernames}")
    private String superUsernames;

    @Override
    public UserDTO getUserInfo(User user) {
        List<String> superNameList = Arrays.stream(superUsernames.split(",")).collect(Collectors.toList());
        UserDTO userDTO = new UserDTO();
        user.setPwd("");
        userDTO.setUser(user);


        if (superNameList.contains(user.getName())) {
            List<GroupUser> list = groupUserService.lambdaQuery().eq(GroupUser::getIsDel, IsDel.N.getCode()).list();
            if (!CollectionUtils.isEmpty(list)) {
                List<Group> groupList = groupService.lambdaQuery().in(Group::getId, list.stream().map(GroupUser::getGroupId).collect(Collectors.toList())).eq(Group::getIsDel, IsDel.N.getCode()).list();
                userDTO.setGroupList(groupList);
            }

            List<UserRole> userRoleList = userRoleService.lambdaQuery().eq(UserRole::getIsDel, IsDel.N.getCode()).list();
            if (!CollectionUtils.isEmpty(userRoleList)) {
                List<Role> roles = roleService.listByIds(userRoleList.stream().map(UserRole::getRoleId).collect(Collectors.toList()));
                userDTO.setRoleList(roles);
            }

            List<Menu> menuList = menuService.lambdaQuery().eq(Menu::getIsDel, IsDel.N.getCode()).list();
            // TO Tree
            List<MenuTree> menuTreeList = MenuTree.getTree(menuList);
            userDTO.setMenuList(menuTreeList);
            userDTO.setMenuCodeList(menuList.stream().map(Menu::getCode).collect(Collectors.toList()));
        } else {

            // group
            List<GroupUser> groupUserList = groupUserService.lambdaQuery().eq(GroupUser::getUserId, user.getId()).eq(GroupUser::getIsDel, IsDel.N.getCode()).list();
            if (!CollectionUtils.isEmpty(groupUserList)) {
                List<Group> groupList = groupService.lambdaQuery().in(Group::getId, groupUserList.stream().map(GroupUser::getGroupId).collect(Collectors.toList())).eq(Group::getIsDel, IsDel.N.getCode()).list();
                userDTO.setGroupList(groupList);
            }

            // role
            List<UserRole> userRoleList = userRoleService.lambdaQuery().eq(UserRole::getUserId, user.getId()).eq(UserRole::getIsDel, IsDel.N.getCode()).list();

            if (!CollectionUtils.isEmpty(userRoleList)) {
                List<Role> roles = roleService.listByIds(userRoleList.stream().map(UserRole::getRoleId).collect(Collectors.toList()));
                userDTO.setRoleList(roles);
            }

            if (!CollectionUtils.isEmpty(userDTO.getRoleList())) {
                // menu
                List<RoleMenu> roleMenuList = roleMenuService.lambdaQuery()
                        .in(RoleMenu::getRoleId, userDTO.getRoleList().stream()
                                .map(role -> role.getId()).collect(Collectors.toList())).eq(RoleMenu::getIsDel, IsDel.N.getCode()).list();


                if (!CollectionUtils.isEmpty(roleMenuList)) {
                    List<Menu> menus = menuService.listByIds(roleMenuList.stream().map(x -> x.getMenuId()).collect(Collectors.toList()));
                    // TO Tree
                    List<MenuTree> menuTreeList = MenuTree.getTree(menus);
                    userDTO.setMenuList(menuTreeList);
                    userDTO.setMenuCodeList(menus.stream().map(Menu::getCode).collect(Collectors.toList()));
                }
            }

        }
        return userDTO;
    }


}
